/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.alphawavesgames.jdroidengine;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import com.alphawavesgames.engine.game.EngineInitListener;
import com.alphawavesgames.engine.game.GameContainer;
import com.alphawavesgames.engine.game.GameThread;
import com.alphawavesgames.engine.rendering.GLRenderContainer;

/**
 *
 * @author dpsevilyn
 */
public class MainActivity extends Activity implements EngineInitListener
{
    protected JAWGLSurfaceView    m_glSurfaceView;
    //RenderUpdateThread  m_renderUpdateThread;
    protected GLRenderContainer   m_renderContainer;
    protected GameThread          m_gameThread;

    protected GameContainer       m_gameContainer;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // turn off the window's title bar.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        //int width = display.getWidth();
        //int height = display.getHeight();

        //create the engine components
        m_renderContainer = GLRenderContainer.createRenderContainer( this );
        //m_gameContainer = new GameContainer();
        m_gameContainer = new TestSprite();
        //initialise engine
        m_renderContainer.initialise();

        //create all the main threads ( game, render update, and renderer )
        m_gameThread = new GameThread( m_gameContainer, m_renderContainer );
        m_gameThread.initialise( m_renderContainer.getRenderHWInterface() );
        //m_renderUpdateThread = new RenderUpdateThread( m_renderContainer );
        //m_renderUpdateThread.initialise( m_renderContainer.getRenderHWInterface() );

        m_glSurfaceView = new JAWGLSurfaceView( this, m_renderContainer );
        setContentView( m_glSurfaceView );

        //register to get a callback when the engine is fully initialised
        //todo - must be a better way.
        //m_renderContainer.registerOnInitListener( this );

        if( savedInstanceState == null )
        {
            // we were just launched: set up a new game
            Log.w(this.getClass().getName(), "SIS is null");
        } 
        else
        {
            // we are being restored: resume a previous game
            //mACFThread.restoreState(savedInstanceState);
            Log.w(this.getClass().getName(), "SIS is nonnull");
        }

        //m_renderUpdateThread.start();
        m_gameThread.start();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();

        //release everything - complete shutdown
    }

    /**
     * Notification that something is about to happen, to give the Activity a
     * chance to save state.
     *
     * @param outState a Bundle into which this Activity should save its state.
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //mACFThread.saveState(outState);
        Log.w(this.getClass().getName(), "SIS called");
    }

    @Override
    protected void onStart()
    {
        super.onStart();
        //recreate all resources
    }

    @Override
    protected void onStop()
    {
        super.onStop();
        //no longer visible - release all resources
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        //m_glSurfaceView.onPause();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        //m_glSurfaceView.onResume();
    }

    public void onEngineInitialised()
    {
        //what should we do to check this is not called while the app is paused
        //or shutting down?
        //m_gameThread.start();
    }

}
